package com.springcloud.alibaba.service.impl;

import com.springcloud.alibaba.dao.OrderDao;
import com.springcloud.alibaba.service.AccountService;
import com.springcloud.alibaba.service.OrderService;
import com.springcloud.alibaba.service.StorageService;
import com.springcloud.alibaba.vo.Order;
import com.springcloud.configclient.commons.entities.CommonResult;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@GlobalTransactional(name = "gt",rollbackFor = Exception.class)
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao dao;
    @Autowired
    private AccountService accountService;
    @Autowired
    private StorageService storageService;

    @Override
    public void createOrder(Order order) {
        System.out.println("开始新建订单");
        dao.createOrder(order);


        System.out.println();
        System.out.println("减库存");
        CommonResult commonResult = storageService.decreseStorage(order.getProductId(), order.getCount());
        System.out.println(commonResult.toString());

        System.out.println();
        System.out.println("扣钱");
        CommonResult commonResult1 = accountService.decreseAccount(order.getUserId(), order.getMoney());
        System.out.println(commonResult1.toString());
        //更新订单

        System.out.println();
        System.out.println("更新订单");
       // int k=9/0;
        dao.updateOrder(order.getUserId(), 1);
    }
}
